import request from '@/utils/request'

// 组织信息
export interface Organization {
  id: number
  parentId: number
  orgName: string
  orgCode: string
  orgType: number
  orgLevel: number
  managerId?: number
  contact?: string
  phone?: string
  email?: string
  address?: string
  remark?: string
  orderNum?: number
  status: number
  createTime?: string
  updateTime?: string
  children?: Organization[]
}

// 组织查询参数
export interface OrganizationQueryParams {
  page?: number
  pageSize?: number
  orgName?: string
  status?: number
}

// 创建组织请求
export interface CreateOrganizationRequest {
  parentId: number
  orgName: string
  orgCode: string
  orgType: number
  orgLevel: number
  managerId?: number
  contact?: string
  phone?: string
  email?: string
  address?: string
  remark?: string
  orderNum?: number
  status: number
}

// 更新组织请求
export interface UpdateOrganizationRequest {
  parentId: number
  orgName: string
  orgCode: string
  orgType: number
  orgLevel: number
  managerId?: number
  contact?: string
  phone?: string
  email?: string
  address?: string
  remark?: string
  orderNum?: number
  status: number
}

// 获取组织树
export function getOrganizationTree() {
  return request.get<Organization[]>('/organizations/tree')
}

// 获取组织列表
export function getOrganizationList(params?: OrganizationQueryParams) {
  return request.get<{
    list: Organization[]
    total: number
    page: number
    size: number
  }>('/organizations', params)
}

// 获取组织详情
export function getOrganizationById(id: number) {
  return request.get<Organization>(`/organizations/${id}`)
}

// 创建组织
export function createOrganization(data: CreateOrganizationRequest) {
  return request.post<Organization>('/organizations', data)
}

// 更新组织
export function updateOrganization(id: number, data: UpdateOrganizationRequest) {
  return request.put<Organization>(`/organizations/${id}`, data)
}

// 删除组织
export function deleteOrganization(id: number) {
  return request.delete(`/organizations/${id}`)
}
